package day14.homework02;

/**
 * @author Xiao
 * @date 2021/8/7
 */
public class HuBoTest03 {
    /**
     * 这是胡波的Test03
     */
    public static void main(String[] args) {
        F f = new F();
        C c = new C();
        f.start();
        c.start();


    }
}

class Deck {
    public static Object obj = new Object();

    public static boolean flag = false;

    public static int hb = 30;
}

class F extends Thread {
    @Override
    public void run() {
        while (true) {
            synchronized (Deck.obj) {
                if (Deck.hb <= 0) {
                    break;
                } else {
                    /**修改了逻辑：如果flag为true：吃货吃汉堡，且数量--，否则wait*/
                    if (Deck.flag) {
                        System.out.println("吃货正在吃汉堡!");
                        Deck.hb--;
                        Deck.flag = false;
                        Deck.obj.notify();
                    } else {
                        try {
                            Deck.obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

class C extends Thread {
    @Override
    public void run() {
        while (true) {
            synchronized (Deck.obj) {
                if (Deck.hb <= 0) {
                    break;
                } else {
                    /**修改了逻辑：如果flag为true：厨师wait，否则厨师生产*/
                    if (Deck.flag) {
                        try {
                            Deck.obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("厨师正在生产汉堡!");
                        Deck.flag = true;
                        Deck.obj.notify();
                    }
                }
            }
        }
    }
}
